package com.beauty.dressing.module.main.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseRecyclerViewHolder;
import com.beauty.dressing.common.bean.BaseBean;
import com.beauty.dressing.common.bean.GroupNavigation;
import com.beauty.dressing.common.bean.Post;
import com.beauty.dressing.common.bean.TopicTitle;
import com.beauty.dressing.module.common.adapter.CommonExpandWrapperAdapter;
import com.beauty.dressing.module.community.adapter.CommunityAdapter;
import com.beauty.dressing.module.community.holder.PostsViewHolder;
import com.beauty.dressing.module.community.holder.TopicTopHolder;

import java.util.List;

/**
 * 类描述
 *
 * @author xutingz
 * @email：914603097@qq.com
 */
public class PostsSearchAdapter extends CommonExpandWrapperAdapter {
    public static final int GROUP_TOP = 0;    // 顶部列表组
    public static final int GROUP_POSTS = 1; // 帖子组


    /** 标题导航 */
    public static final int VIEW_TYPE_GROUP = 100;
    /** 论坛帖子 */
    public static final int VIEW_TYPE_POSTS = 101;
    public static final int VIEW_TYPE_TOPICS = 102;

    public PostsSearchAdapter(Context context) {
        super(context);
        addGroup(new GroupNavigation(0,"",true,false));//(第一组)
        addGroup(new GroupNavigation(1,"帖子",true,false));//(第二组)
    }

    /**
     *
     */
    public void setTopList(List<BaseBean> list) {
        if (list != null && !list.isEmpty()) {
            setGroupShow(GROUP_TOP, false,false);
        } else {
            setGroupShow(GROUP_TOP, false,false);
        }
        clearGroupChild(GROUP_TOP);
        addGroupChild(GROUP_TOP,list);
    }

    public void setPostList(List<BaseBean> list) {
        if (list != null && !list.isEmpty()) {
            setGroupShow(GROUP_POSTS, false,false);
        } else {
            setGroupShow(GROUP_POSTS, false,false);
        }
        clearGroupChild(GROUP_POSTS);
        addGroupChild(GROUP_POSTS,list);
    }


    public void setGroupShow(int group, boolean isShow,boolean showMore){
        BaseBean bean =  getGroup(group);
        if (bean != null && bean instanceof GroupNavigation) {
            GroupNavigation titleNav = (GroupNavigation)bean;
            titleNav.isVisibility = isShow;
            titleNav.showMore = showMore;
            updateGroup(group, titleNav);
        }
    }

    /**
     * 给对应的分组添加数据在后面
     * @param list
     */
    public void addList(int group,List<BaseBean> list) {
        addGroupChild(group, list);
    }

    public void removeGroup(){
        clearGroupChild(GROUP_POSTS);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder = null;
        if (isHeader(viewType) || isFooter(viewType)) {
            return super.onCreateViewHolder(parent, viewType);
        } else {
            switch (viewType) {
                case VIEW_TYPE_POSTS:
                    PostsViewHolder postsReplyViewHolder = new PostsViewHolder(mContext, mLayoutInflater, parent);
                    viewHolder = postsReplyViewHolder;
                    break;
                case VIEW_TYPE_GROUP:
                    viewHolder = new GroupNavHolder(mContext, mLayoutInflater, parent);
                    break;
                case VIEW_TYPE_TOPICS:
                    viewHolder = new TopicTopHolder(mContext, mLayoutInflater, parent);
                    break;
                default:
                    viewHolder = super.onCreateViewHolder(parent, viewType);
                    break;
            }
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        super.onBindViewHolder(holder, position);
    }

    @Override
    public int getItemViewType(int position) {
        return getItemViewType(getItem(position));
    }

    @Override
    public int getItemViewType(BaseBean bean) {
        int type = 0;
        if (bean instanceof FixedViewInfo) {
            return ((FixedViewInfo) bean).viewType;
        } else if (bean instanceof Post) {
            type = VIEW_TYPE_POSTS;
        } else if (bean instanceof TopicTitle){
            type = VIEW_TYPE_TOPICS;
        } else if (bean instanceof GroupNavigation){
            type = VIEW_TYPE_GROUP;
        } else {
            type = super.getItemViewType(bean);
        }
        return type;
    }

    public void clearAllList(){
        removeGroup();
    }

    /**
     * 分组的Holder
     */
    public class GroupNavHolder extends BaseRecyclerViewHolder<GroupNavigation> {

        private TextView mTitleTxt;
        private TextView mMoreTxt;

        public GroupNavHolder(Context mContext, LayoutInflater mInflater, ViewGroup viewGroup) {
            super(R.layout.market_group_nav, mContext, mInflater, viewGroup);
        }

        @Override
        public void findView() {
            mTitleTxt = (TextView) itemView.findViewById(R.id.title_txt);
            mMoreTxt = (TextView) itemView.findViewById(R.id.more_txt);
        }

        @Override
        public void bindData(final GroupNavigation bean, int position) {
            setVisibility(bean.isVisibility);
            mTitleTxt.setText(bean.title);
            mMoreTxt.setVisibility(bean.showMore? View.VISIBLE:View.GONE);
            mMoreTxt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (bean.groupNum != 2) {
                    }
                }
            });
        }

        public void setVisibility(boolean isVisibility){
            itemView.getLayoutParams().height = isVisibility ? ViewGroup.LayoutParams.WRAP_CONTENT : 0;
            itemView.requestLayout();
        }
    }


}
